<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>News Results</title>
<style type="text/css">
#news {border:1%;, cellpadding:3%; cellspacing:3%;}
</style>
<script type="text/javascript">
</script>
<%@page import="java.util.ArrayList" %>
<%@page import="beans.Articles" %>
</head>
<body >
<div align="center">
<h1 align="center"> Demonstration of the Pagination </h1>
<form action="news" method="get" >
<input  type="text" name="st" id="st" placeholder="StateCode" list="state">
<datalist id="state">
  <option value="CA">
  <option value="NY">
  <option value="AZ">
</datalist>

<input type="Submit" value ="News Results">
</form>
</div>
<br>
<c:if test="${ not empty newsList}">


    <table id="news" align="center" border="1" cellpadding="5" cellspacing="5">
        <tr>
            <th>State ID</th>
            <th>State Name</th>
            <th>News</th>
            <th>News Link</th>
        </tr>
</c:if>
        <c:forEach var="news" items="${newsList}">
            <tr>
                <td>${news.id}</td>
                <td>${news.stateid}</td>
                  <td>${news.news}</td>
                  <td><a href="${news.link}">${news.link}</a></td>
            </tr>
        </c:forEach>
    </table>
<br>
  <div align="center">
    <%--For displaying Previous link except for the 1st page --%>
    <c:if test="${currentPage != 1 and currentPage !=null }">
        <td><a href="news?st=<%= request.getParameter("st")%>&page=${currentPage - 1}">Previous</a></td>
    </c:if>
  </div>
    <%--For displaying Page numbers.
    The when condition does not display a link for the current page--%>
    <table align="center" border="1" cellpadding="5" cellspacing="5">
        <tr>
            <c:forEach begin="1" end="${noOfPages}" var="i">
                <c:choose>
                    <c:when test="${currentPage eq i}">
                        <td>${i}</td>
                    </c:when>
                    <c:otherwise>
                        <td><a href="news?st=<%= request.getParameter("st")%>&page=${i}">${i}</a></td>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
        </tr>
    </table>
 
 <div align="center">
    <%--For displaying Next link --%>
    <c:if test="${currentPage lt noOfPages}">
        <td><a href="news?st=<%= request.getParameter("st")%>&page=${currentPage + 1}">Next</a></td>
    </c:if>
 </div>
</body>
</html>
